﻿using System.Collections.Generic;
using DataFiller.DataStorage.Extension.Interpreters;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace DataFillerTest
{
    [TestClass]
    public class InterpretersTest
    {
        object[] DataModel;

        [TestInitialize]
        public void PrepareData()
        {
            DataModel = new object[1];

            TestRow Model = new TestRow();
            Model.RowNumber = 8556;

            Model.Cells = new List<TestCell>();
            for (int i = 0; i < 100; i++)
            {
                TestCell TestData = new TestCell
                {
                    CellNumber = i,
                    Data = string.Format("Data {0}", i),
                    Style = string.Format("Style {0}", i),
                };

                TestData.TestProperty = new InnerType
                {
                    Property1 = i,
                    Property2 = new InnerType
                    {
                        Property1 = i * i,
                        Property2 = null,
                        Property3 = null
                    }
                };

                Model.Cells.Add(TestData);
            }

            DataModel[0] = Model;
        }

        [TestMethod]
        public void ColInterpreter_Test()
        {
            //correct
            ColPlaceHolder_Interpreter interpreper = new ColPlaceHolder_Interpreter();
            var matchResult = interpreper.MatchCase("(col)=[0].Cells&TestProperty.Property1");
            Assert.IsNotNull(matchResult);
            dynamic retrieveResult = matchResult(DataModel);
            Assert.AreEqual(retrieveResult.Orientation, ExtensionDirection.Vertical);
            Assert.IsNotNull(retrieveResult.Data);

            matchResult = interpreper.MatchCase("(col)=&a");
            Assert.IsNotNull(matchResult);

            //incorrect
            matchResult = interpreper.MatchCase("(col)=a&");
            Assert.IsNull(matchResult);

            matchResult = interpreper.MatchCase("(col)=a");
            Assert.IsNull(matchResult);

            matchResult = interpreper.MatchCase("a(col)=a");
            Assert.IsNull(matchResult);

            matchResult = interpreper.MatchCase("a(acol)=a");
            Assert.IsNull(matchResult);

            matchResult = interpreper.MatchCase("a(ol)=a");
            Assert.IsNull(matchResult);
        }

        [TestMethod]
        public void RowInterpreter_Test()
        {
            //correct
            RowPlaceHolder_Interpreter interpreper = new RowPlaceHolder_Interpreter();
            var matchResult = interpreper.MatchCase("(row)=[0].Cells&TestProperty.Property1");
            Assert.IsNotNull(matchResult);
            dynamic retrieveResult = matchResult(DataModel);
            Assert.AreEqual(retrieveResult.Orientation, ExtensionDirection.Horizontal);
            Assert.IsNotNull(retrieveResult.Data);

            matchResult = interpreper.MatchCase("(row)=&a");
            Assert.IsNotNull(matchResult);

            //incorrect
            matchResult = interpreper.MatchCase("(row)=a&");
            Assert.IsNull(matchResult);

            matchResult = interpreper.MatchCase("(row)=a");
            Assert.IsNull(matchResult);

            matchResult = interpreper.MatchCase("a(row)=a");
            Assert.IsNull(matchResult);

            matchResult = interpreper.MatchCase("a(arow)=a");
            Assert.IsNull(matchResult);

            matchResult = interpreper.MatchCase("a()=a");
            Assert.IsNull(matchResult);
        }

        [TestMethod]
        public void FillInterpreter_Test()
        {
            //correct
            FillPlaceHolder_Interpreter interpreper = new FillPlaceHolder_Interpreter();
            var matchResult = interpreper.MatchCase("(fill)=[0].RowNumber");
            Assert.IsNotNull(matchResult);
            dynamic retrieveResult = matchResult(DataModel);
            Assert.AreEqual(retrieveResult.Orientation, ExtensionDirection.None);
            Assert.AreEqual(retrieveResult.Data, 8556);


            matchResult = interpreper.MatchCase("(fill)=&a");
            Assert.IsNotNull(matchResult);

            //incorrect
            matchResult = interpreper.MatchCase("(fill)=a&");
            Assert.IsNotNull(matchResult);

            matchResult = interpreper.MatchCase("(fill)=a");
            Assert.IsNotNull(matchResult);

            matchResult = interpreper.MatchCase("a(fill)=a");
            Assert.IsNull(matchResult);

            matchResult = interpreper.MatchCase("a(afill)=a");
            Assert.IsNull(matchResult);

            matchResult = interpreper.MatchCase("a()=a");
            Assert.IsNull(matchResult);
        }
    }
}
